package com.android.canvasproject.view.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.canvasproject.R;
import com.android.canvasproject.adapter.CanvasTypeSpinAdapter;
import com.android.canvasproject.enums.CanvasTypeEnum;
import com.android.canvasproject.model.dao.ProviderDaoOpenHelper;
import com.android.canvasproject.model.entity.Canvas;
import com.android.canvasproject.view.utils.ComponentViewUtil;

public class AddCanvasDialog extends DialogFragment {

	private CanvasTypeSpinAdapter adapter;
	private Spinner spinner;
	private View view;
	private CanvasTypeEnum canvasTypeSelected;
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		view = inflater.inflate(R.layout.dialog_add_canvas, null);

		canvasTypeSelected = CanvasTypeEnum.PROJECT_MODEL_CANVAS;

		builder.setView(view);
		builder.setPositiveButton(R.string.button_positive_save_dialog, null);
		builder.setNegativeButton(R.string.button_negative_cancele_dialog, null);
		
		adapter = new CanvasTypeSpinAdapter(getActivity(), android.R.layout.simple_spinner_item, CanvasTypeEnum.values());
		spinner = (Spinner) view.findViewById(R.id.spinner_canvas_type);
		spinner.setAdapter(adapter);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
				canvasTypeSelected = adapter.getItem(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) { }
		});
		
		return builder.create();
	}
	
	@Override
	public void onStart() {
		super.onStart();

		AlertDialog form = (AlertDialog) getDialog();
		if(form != null){
			Button buttonPositive = form.getButton(Dialog.BUTTON_POSITIVE);
			buttonPositive.setBackgroundResource(R.drawable.selector_button_common);
			buttonPositive.setOnClickListener(new AddCanvasDialogListener());

			Button buttonNegative = form.getButton(Dialog.BUTTON_NEGATIVE);
			buttonNegative.setBackgroundResource(R.drawable.selector_button_common);
		}
	}

	class AddCanvasDialogListener implements View.OnClickListener, View.OnFocusChangeListener {

		@Override
		public void onClick(View v) {
			EditText title = (EditText) view.findViewById(R.id.textview_title_canvas);
			EditText detail = (EditText) view.findViewById(R.id.textview_detail_canvas);

			if(title.getText().toString() != null && !title.getText().toString().isEmpty()){
				ContentValues values = new ContentValues();
				values.put("title", title.getText().toString());
				values.put("detail", detail.getText().toString());
				values.put("canvas_type", canvasTypeSelected.ordinal());
				values.put("id_person_sponsor", 1);

				ProviderDaoOpenHelper provider = new ProviderDaoOpenHelper(getActivity());
				Canvas canvas = new Canvas();
				canvas.setSponsor(1); //ALTERAR APÓS DEFINIR LOCAL DE INFORMAÇÂO DO USUARIO LOCAL
				canvas.setCanvasType(canvasTypeSelected);
				canvas.setDetail(detail.getText().toString());

				canvas.setId(provider.save(Canvas.CanvasDao.TB_CANVAS, values));

				if(canvasTypeSelected.equals(CanvasTypeEnum.PROJECT_MODEL_CANVAS)){
					LinearLayout layoutProjects = ComponentViewUtil.createNewCanvas(getActivity(), title.getText().toString(), detail.getText().toString(), canvasTypeSelected, canvas);
					MyCanvasFragment.blockProjectsLayout.addView(layoutProjects, 0);

				} else if(canvasTypeSelected.equals(CanvasTypeEnum.BUSINESS_MODEL_CANVAS)){
					LinearLayout layoutBusiness = ComponentViewUtil.createNewCanvas(getActivity(), title.getText().toString(), detail.getText().toString(), canvasTypeSelected, canvas);
					MyCanvasFragment.blockBusinessLayout.addView(layoutBusiness, 0);

				} else if(canvasTypeSelected.equals(CanvasTypeEnum.ANALISYS_SWOT)){
					Toast.makeText(getActivity(), "[ANALISYS SWOT] Projeto não implementado!", Toast.LENGTH_LONG).show();
					
				} else if(canvasTypeSelected.equals(CanvasTypeEnum.KANBAN)){
					Toast.makeText(getActivity(), "[KANDAN] Projeto não implementado!", Toast.LENGTH_LONG).show();
					
				}

				provider.cleanup();
				dismiss();

			} else {
				title.setError(getResources().getString(R.string.field_required_warning));
				title.setOnFocusChangeListener(this);
				return;
			}
		}

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			EditText title = (EditText)v;

			if(title.getText().toString() != null && !title.getText().toString().isEmpty()){
				title.setError(null);
			}
		}
	}
}